package com.kingdee.eas.custom.common.client;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.ISQLExecutor;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.base.permission.OrgRangeCollection;
import com.kingdee.eas.base.permission.OrgRangeType;
import com.kingdee.eas.base.permission.util.PermissionRangeHelper;
import com.kingdee.eas.basedata.org.AdminOrgUnitCollection;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.jdbc.rowset.IRowSet;

public class AdminOrgUtils {

	public static int ORG_TYPE = 20;

	public static AdminOrgUnitCollection getCurrentAdminRange()
			throws Exception {
		EntityViewInfo view = getAdminOrgRangeEntityViewInfo();
		return getRemoteInstance().getAdminOrgUnitCollection(view);
	}

	public static AdminOrgUnitInfo getCurrentDefaultAdminRange()
			throws Exception {
		AdminOrgUnitCollection adminOrgs = getCurrentAdminRange();
		if (adminOrgs.size() > 0) {
			return adminOrgs.get(0);
		}
		return null;
	}

	private static EntityViewInfo getAdminOrgRangeEntityViewInfo()
			throws EASBizException, BOSException {
		String userId = SysContext.getSysContext().getCurrentUserInfo().getId()
				.toString();

		OrgRangeCollection orgRangeCollection = PermissionRangeHelper
				.getUserOrgRange(null, new ObjectUuidPK(userId),
						OrgRangeType.ADMIN_ORG_TYPE);
		Set set = new HashSet();
		for (int i = 0; i < orgRangeCollection.size(); i++) {
			set.add(orgRangeCollection.get(i).getOrg().getId().toString());
		}
		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("id", set, CompareType.INCLUDE));

		EntityViewInfo view = new EntityViewInfo();
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("*");
		view.setFilter(fi);
		return view;
	}

	public static FilterInfo getOutOrgRangePersonFilter(String colPerson,
			AdminOrgUnitInfo orgUnit, boolean isInclude) throws Exception {
		FilterInfo fi = new FilterInfo();

		if (orgUnit == null) {
			fi.getFilterItems().add(new FilterItemInfo(colPerson, null));
			return fi;
		}

		String userID = SysContext.getSysContext().getCurrentUserInfo().getId()
				.toString();
		String hrOrgUnitID = SysContext.getSysContext().getCurrentHRUnit()
				.getId().toString();

		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT P.FID FROM T_BD_PERSON P LEFT JOIN T_ORG_ADMIN ORG ON P.CFDEPTID=ORG.FID");
		sql.append(" WHERE ");
		sql.append(" P.CFDEPTID IN ");
		sql.append(" ( ");

		sql
				.append(" SELECT FFROMUNITID FROM T_ORG_UNITRELATION WHERE FTOUNITID = ");
		sql.append("'").append(hrOrgUnitID).append("'");
		sql
				.append(" AND FTYPERELATIONID = '00000000-0000-0000-0000-0000000000100FE9F8B5'");
		sql.append(" AND FFromUnitID IN ");
		sql.append("(");
		sql.append(" SELECT FOrgID FROM T_PM_OrgRange ");
		sql.append(" WHERE FUserID = ");
		sql.append("'").append(userID).append("'");
		sql.append(" and FType = 20 ");
		sql.append(")");
		sql.append(")");

		if (orgUnit != null) {
			if (isInclude) {
				sql.append(" and ");
				sql.append(" ( ");
				sql.append(" org.FLongNumber = '").append(
						orgUnit.getLongNumber()).append("'");
				sql.append(" or org.FLongNumber like '").append(
						orgUnit.getLongNumber()).append("%'");
				sql.append(" ) ");
			} else {
				sql.append(" and org.FLongNumber = '").append(
						orgUnit.getLongNumber()).append("'");
			}
		}

		ISQLExecutor exec = SQLExecutorFactory
				.getRemoteInstance(sql.toString());
		IRowSet rs = null;
		List listIds = new ArrayList();
		rs = exec.executeSQL();
		while (rs.next()) {
			if (rs.getString(1) == null)
				continue;
			listIds.add(rs.getString(1));
		}
		if (listIds.size() > 0) {
			fi.getFilterItems().add(
					new FilterItemInfo(colPerson, new HashSet(listIds),
							CompareType.INNER));
		} else {
			listIds.add("");
			fi.getFilterItems().add(
					new FilterItemInfo(colPerson, new HashSet(listIds),
							CompareType.INNER));
		}

		return fi;
	}

	private static IAdminOrgUnit getRemoteInstance() throws Exception {
		return AdminOrgUnitFactory.getRemoteInstance();
	}
}
